﻿@using Classified.Data.Enums
@using Classified.Domain.Entities
@using Classified.Service
@model IEnumerable<Classified.Domain.Entities.ClassifiedCategoryAttribute>
@{ List<string> list = ViewBag.filterBy;
   var data = new AttributeValue();
   var MultipleChoice = (int)AttributeSearchBy.MultipleChoice;
   var Range = (int)AttributeSearchBy.Range;
}
<script type="text/javascript">


    $(".Cate_val").live("click", function () {
        var id = $(this).attr("data-id");
        var cat_name = $(this).attr("data-name");
        $("form.ClassifiedAction").attr("id", id);
        $("form.ClassifiedAction").attr("seName", cat_name);
       var url = "/classified/" + id + "/" + cat_name;
        $("form.ClassifiedAction").attr("action", url);
        $("form.ClassifiedAction").submit();
        //        $.get("/classified/" + id + "/" + cat_name);

    });
</script>
<div class="row well">
    @using (Html.BeginForm("ClassifiedList", "ClassifiedAds", FormMethod.Get, new { @class = "ClassifiedAction" }))
    {
        <ul class="nav nav-list">
            <div style="display: none">
                <input type="submit" id="frmSubmit" /></div>
            <li class="nav-header"><b>@ViewBag.CategoryName Categories</b></li>
            @{
        List<ClassifiedCategory> categories = ViewBag.Categories;
            }
            @if (categories.Count() > 0 || categories != null)
            {

                foreach (var catval in categories)
                {
                    
                <li><a href="javascript:void(0);" data-id ="@catval.Id" data-name="@catval.Name" class="Cate_val">@catval.Name</a></li>
               
               
                }
            }
             <li class="divider"></li>
            @foreach (var item in Model)
            {
                var searchBy = item.AttributesSearchBy;
                
                                <li class="nav-header"><b>@item.Attr_Label</b></li>
                            
                switch (searchBy)
                {

                    case 1: //filter by Multiple Choice
                        {

                            if (list.Count > 0)
                            {

                                foreach (var val in list)
                                {
                                    var id = Convert.ToInt32(val);
                                    data = item.AttributeValues.Find(x => x.Id == id);
                                    if (data != null)
                                    {
                                        break;
                                    }

                                }
                            }
                            if (data != null && (data.Id != 0))
                            {
                                <li>
                                    <label class="checkbox">
                                    <input type="checkbox" id="@data.Id" name="@item.Attr_Name" checked="checked" value="@data.Id"/>@data.Value
                               </label> </li>

                               
                                <script type="text/javascript">

                                    $("#" + @data.Id).live("click", function() {
                                        $("#frmSubmit").click();

                                    });                           
                        
                                </script>

                            }
                            else
                            {

                                foreach (var items in item.AttributeValues)
                                {

                                    <li>
                                        <label class="checkbox">
                                        <input type="checkbox" id="@items.Id" name="@item.Attr_Name" value="@items.Id"/>@items.Value
                                 </label>   </li>

                                    
                                    <script type="text/javascript">
                                        var url = "@Url.Action("SearchByAttributes")";
                                        $("#" + @items.Id).live("click", function() {
                                            $("#frmSubmit").click();

                                        });
                                    </script>
                                }
                            }
                        }
                        break;
                    case 2: // filter by Range
                        {
                            var name1 = item.Attr_Name + "_1";
                            var name2 = item.Attr_Name + "_2";
                            var id = item.Attr_Name + "_id";
                            <li class="input-append">
                                <input type="text" placeholder="Min" name="@name1"  class="input-mini"/>&nbsp;-&nbsp;<input type="text" placeholder="Max" name="@name2" class="input-mini"/><button class="btn" id="@id" type="button">Go!</button>
                                
                            </li>
                            <script>
                                $("#@id").live("click", function() {
                                    
                                            $("#frmSubmit").click();

                                        });
                            </script>
                        }
                        break;
                }
                <div class="clear"></div>
<li class="divider"></li>
            }
            
        </ul>
    }
</div>
